/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.pdsu.alumni.common.exception;


import com.auth0.jwt.exceptions.TokenExpiredException;
import com.pdsu.alumni.common.result.Result;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.mybatis.spring.MyBatisSystemException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.sql.SQLSyntaxErrorException;


/**
 * 异常处理器
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestControllerAdvice
public class RRExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(RRException.class)
    public Result handleRRException(RRException e) {
        Result r = new Result();
        r.setCode(e.getCode());
        r.setMsg(e.getMsg());
        return r;
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public Result handlerNoFoundException(Exception e) {
        logger.error(e.getMessage(), e);
        return new Result().error(404, "路径不存在，请检查路径是否正确");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public Result handleDuplicateKeyException(DuplicateKeyException e) {
        logger.error(e.getMessage(), e);
        return new Result().error("数据库中已存在该记录");
    }

    @ExceptionHandler({UnauthorizedException.class, AuthorizationException.class})
    public Result handleAuthorizationException(AuthorizationException e) {
        logger.error(e.getMessage(), e);
        return new Result().error(403, "没有权限，请联系管理员授权");
    }

    @ExceptionHandler(TokenExpiredException.class)
    public Result handleTokenExpiredException(TokenExpiredException e) {
        logger.error(e.getMessage(), e);
        return new Result().error("身份失效,请重新登陆");
    }

    @ExceptionHandler({AuthenticationException.class, UnauthenticatedException.class})
    public Result handleAuthenticationException(AuthenticationException e) {
        logger.error(e.getMessage(), e);
        return new Result().error(401, "未登录,请登陆后操作");
    }

    @ExceptionHandler(SQLSyntaxErrorException.class)
    public Result SQLSyntaxErrorException(SQLSyntaxErrorException e) {
        logger.error(e.getMessage(), e);
        return new Result().error("db操作异常");
    }

    @ExceptionHandler(MyBatisSystemException.class)
    public Result SQLSyntaxErrorException(MyBatisSystemException e) {
        logger.error(e.getMessage(), e);
        return new Result().error("mybatis异常");
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        logger.error(e.getMessage(), e);
        return new Result().error("服务器异常");
    }
}
